package com.SMP.controller.class_room;

import com.SMP.model.Course;
import com.SMP.model.Msg;
import com.SMP.service.BroadcastService;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;

@Controller
public class FileController {

    @Autowired
    BroadcastService broadcastService;

    /**
     * 获取资源
     * @author WYHY
     *
     * */
    @RequestMapping("/getPhoto")
    @ResponseBody
    public Msg getFileName(){
        List<String> result=broadcastService.getFileName("Carousel/");
        if (result.size()==0){
            return Msg.fail();
        }else{
            return Msg.success().add("filename",result);
        }
    }

    @RequestMapping("/image")
    @ResponseBody
    public void getImage(HttpServletRequest request, HttpServletResponse response,String path) throws Exception{
        if(!StringUtil.isEmpty(path)){
            String suffix=path.substring(path.lastIndexOf(".")+1,path.length()).toUpperCase();
            String Type="";
            if (suffix.equals("MP4")){
                Type="video/mp4;charset=UTF-8";
            }else if (suffix.equals("GIF")||suffix.equals("JPG")||suffix.equals("PNG")){
                Type="image/"+suffix.toLowerCase()+";charset=UTF-8";
            }
            // 本地文件路径
            String filePath = "D:"+path;
            File file = new File(filePath);
            // 获取输出流
            OutputStream outputStream = response.getOutputStream();
            FileInputStream fileInputStream = new FileInputStream(file);
            // 读数据
            byte[] data = new byte[fileInputStream.available()];
            fileInputStream.read(data);
            fileInputStream.close();
            // 回写
            response.setContentType(Type);
            outputStream.write(data);
            outputStream.flush();
            outputStream.close();
        }



    }


    /**
     * @Description: 删除 文件
     * @Param: [id]
     * @Author: wyhy
     * @Date: 2018/9/30
     */
    @RequestMapping(value = "deleteAnnex",method = RequestMethod.POST)
    @ResponseBody
    private Msg deleteFile(@RequestParam(value = "fileurl",defaultValue = "no")String fileurl){
        boolean state=broadcastService.deleteFile(fileurl);
        if (state){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }

    /**
     * @Description: 上传附件
     * @Param: [file,fileType,annexId,cardId ,request]
     * @Author: WYHY
     * @Date: 2018/9/7
     */
    @RequestMapping(value = "/uploadAnnex",method = RequestMethod.POST)
    @ResponseBody
    public Msg uploadAnnex(@RequestParam("file") MultipartFile file) {
        String result=broadcastService.uploadFile(file,"Carousel/");
        return Msg.success().add("Course",result);
    }
}
